home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.07 Jul 87 / bit map source / notes / trackdrag.p < prev   
Encoding:
Text File  |  1987-03-27  |  5.7 KB  |  190 lines  |  [TEXT/MPS ]

  1. program drag;
  2.  
  3. USES {$LOAD pinterfaces.dump}        
  4.         MemTypes,QuickDraw,OsIntf,PasLibIntf,ToolIntf,
  5.         PackIntf,IntEnv,CursorCtl;
  6.  
  7. var myPic : PicHandle;
  8.     myRect : Rect;
  9.     oldPort: GrafPtr;
  10.  
  11. procedure DragIt( thePicture : PicHandle );
  12. var
  13.     offScreenBits,                {entire screen image}
  14.     underBits,                     {bits obscured by picture}
  15.     pictureBits : BitMap;        {the picture}
  16.  
  17.     updateRegion,                {new position of picture
  18.                                          plus last position}
  19.     thePictureRgn : RgnHandle;    {use this for masking}
  20.     
  21.     wMgrPort,
  22.     oldPort,
  23.     workPort : GrafPtr;
  24.     
  25.     lastRect,                        {this + dragRect => updateRegion}
  26.     otherUpdateRect,            {updateRect clipped to include only bits onscreen}
  27.     updateRect,                    {union of dragRect and lastRect}
  28.     dragRect,                        {size of the picture & centered over cursor}
  29.     tempBounds : Rect;        {used in creating bitmaps}
  30.     
  31.     mousePt,                        {used to position dragRect}
  32.     otherMousePt,                {used to see if cursor has moved}
  33.     testPt : Point;                {used to see if cursor has moved}
  34.     
  35.     synchCount : longint;    {wait for TickCount to change before drawing}
  36.     
  37. begin
  38.     {remember where we are then do all the work in the wMgrPort}
  39.     GetPort( oldPort );
  40.     GetWMgrPort( wMgrPort );
  41.     
  42. {try to allocate and erase the following bitmaps
  43.     - pictureBits holds the bitmap to display
  44.     - underBits holds the bits obscured by the picture
  45.     - offScreenBits holds the entire screen's image
  46. }
  47. {create a bitmap for the picture the size of the picture frame}
  48.     tempBounds := thePicture^^.picFrame;
  49.     with pictureBits, tempBounds do
  50.     begin
  51.         rowBytes := ((right - left + 15) DIV 16) * 2;
  52.         baseAddr := NewPtr(rowBytes * (bottom - top));
  53.         bounds := tempBounds;
  54.         if MemError <> noErr then baseAddr := nil;
  55.         if baseAddr = nil then IEExit(1);
  56.     end;
  57. {home tempBounds before setting up underBits}
  58.     with tempBounds do OffSetRect( tempBounds, -left, -top );
  59.     with underBits, tempBounds do
  60.     begin
  61.         rowBytes := ((right - left + 15) DIV 16) * 2;
  62.         baseAddr := NewPtr(rowBytes * (bottom - top));
  63.         bounds := tempBounds;
  64.         if MemError <> noErr then baseAddr := nil;
  65.         if baseAddr = nil then IEExit(1);
  66.     end;
  67. {now make an offscreen bitmap to hold the whole screen}
  68.     tempBounds := screenBits.bounds;
  69.     with offScreenBits, tempBounds do
  70.     begin
  71.         rowBytes := ((right - left + 15) DIV 16) * 2;
  72.         baseAddr := NewPtr(rowBytes * (bottom - top));
  73.         bounds := tempBounds;
  74.         if MemError <> noErr then baseAddr := nil;
  75.         if baseAddr = nil then IEExit(1);
  76.     end;
  77.  
  78.     workPort := GrafPtr( NewPtr( sizeof(GrafPort) ) );
  79.     if workPort = nil then IEExit(1);
  80.  
  81. {clear out the bitmap for the picture}
  82.     OpenPort( workPort );
  83.     SetPortBits( pictureBits );
  84.     EraseRect( pictureBits.bounds );
  85.     
  86. {create the region to update with}
  87.     updateRegion := NewRgn;
  88.     
  89. {draw the picture into pictureBits & create thePictureRgn}
  90.     thePictureRgn := NewRgn;
  91.     OpenRgn;
  92.         DrawPicture( thePicture, thePicture^^.picFrame );
  93.     CloseRgn( thePictureRgn );
  94.     DrawPicture( thePicture, thePicture^^.picFrame );
  95.     
  96. {the bounding rect to track with the cursor}
  97.     dragRect := pictureBits.bounds;
  98.     
  99. {copy the screen}
  100.     CopyBits( screenBits, offScreenBits, screenBits.bounds, 
  101.             offScreenBits.bounds, srcCopy, nil );
  102.     
  103.     SetPort( wMgrPort );
  104.     SetRect( lastRect, 0,0,0,0 );
  105.     
  106.     while button do
  107.     begin
  108.         GetMouse( mousePt );
  109.         SystemTask;
  110.         otherMousePt := mousePt;
  111.     {home the region and the rect}
  112.         OffSetRgn( thePictureRgn, -dragRect.left, -dragRect.top );
  113.         OffSetRect( dragRect, -dragRect.left, -dragRect.top );
  114.     {center the object over the cursor}
  115.     {calculate mousePt here because we know the rect is home'd}
  116.         mousePt.h := mousePt.h - dragRect.right div 2;
  117.         mousePt.v := mousePt.v - dragRect.bottom div 2;
  118.         OffSetRect( dragRect, mousePt.h, mousePt.v );
  119.         OffSetRgn( thePictureRgn, mousePt.h, mousePt.v );
  120.  
  121.     {save bits underneath}
  122.         {remove any part of dragRect which is off the screen}
  123.         if SectRect( screenBits.bounds, dragRect, updateRect ) then {};
  124.         otherUpdateRect := updateRect;
  125.         with otherUpdateRect do
  126.             OffSetRect( otherUpdateRect, -left, -top );
  127.         CopyBits( offScreenBits, underBits, updateRect, otherUpdateRect, 
  128.                 srcCopy,nil );
  129.     {mask out shape of my object}
  130.         CopyBits( pictureBits, offScreenBits, pictureBits.bounds, dragRect,
  131.                 notSrcBic, thePictureRgn );
  132.     {draw my object}
  133.         CopyBits( pictureBits, offScreenBits, pictureBits.bounds, dragRect,
  134.                 srcOr, thePictureRgn );
  135.  
  136.     {wait for just the right moment}
  137.         synchCount := TickCount;
  138.         repeat until TickCount <> synchCount;
  139.     {update what was on there plus the new position for the object}
  140.         UnionRect( dragRect, lastRect, updateRect );
  141.         RectRgn( updateRegion, updateRect );
  142.     {put it on the screen}
  143.         if SectRect( screenBits.bounds, updateRect, updateRect ) then {};
  144.         RectRgn( updateRegion, updateRect );
  145.         CopyBits( offScreenBits, screenBits, updateRect, updateRect,
  146.                 srcCopy, updateRegion );
  147.  
  148.     {wait until the mouse is moved}
  149.         repeat GetMouse( testPt );
  150.         until not EqualPt( testPt, otherMousePt ) or not button;
  151.     {restore bits underneath}
  152.         {remove any part of dragRect which is off the screen}
  153.         if SectRect( dragRect, screenBits.bounds, updateRect ) then {};
  154.         CopyBits( underBits, offScreenBits, 
  155.                     otherUpdateRect, updateRect, srcCopy, nil );
  156.         lastRect := dragRect;
  157.     end; {while button}
  158.  
  159.     {tidy up}
  160.     CopyBits( offScreenBits, screenBits, offScreenBits.bounds, screenBits.bounds,
  161.                 srcCopy, nil );
  162.     SetPort( oldPort );
  163.     ClosePort( workPort );
  164.     DisposPtr( Pointer(workPort) );
  165.     DisposPtr( pictureBits.baseAddr );
  166.     DisposPtr( underBits.baseAddr );
  167.     DisposPtr( offScreenBits.baseAddr );
  168.     DisposHandle( Handle(thePictureRgn) );
  169.     DisposHandle( Handle(updateRegion) );
  170.  
  171. end; {DragIt}
  172.  
  173. begin{main}
  174.     InitGraf(@thePort);
  175.     GetWMgrPort( oldPort );
  176.     SetPort( oldPort );
  177.     
  178.     ClipRect( screenBits.bounds );
  179.     myPic := PicHandle(GetResource('PICT', 128));
  180.     
  181.     InitCursor;
  182.     repeat until button;
  183.     
  184.     HideCursor;
  185.     DragIt( myPic );
  186.     ShowCursor;
  187.     
  188.     IEExit(0);
  189. end.
  190.